Shader "Custom/ScreenToClean"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BlurStrength ("Blur Strength", Range(0, 1)) = 0.5
        _BlurSize ("Blur Size", Range(0, 10)) = 1.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_TexelSize;
            float _BlurStrength;
            float _BlurSize;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            // 高斯模糊核函数
            fixed4 GaussianBlur(sampler2D tex, float2 uv, float2 texelSize, float blurSize)
            {
                float2 offsets[9] = {
                    float2(-1, -1), float2(0, -1), float2(1, -1),
                    float2(-1,  0), float2(0,  0), float2(1,  0),
                    float2(-1,  1), float2(0,  1), float2(1,  1)
                };
                
                float weights[9] = {
                    1.0/16, 2.0/16, 1.0/16,
                    2.0/16, 4.0/16, 2.0/16,
                    1.0/16, 2.0/16, 1.0/16
                };
                
                fixed4 color = fixed4(0, 0, 0, 0);
                for (int i = 0; i < 9; i++)
                {
                    float2 offset = offsets[i] * texelSize * blurSize;
                    color += tex2D(tex, uv + offset) * weights[i];
                }
                return color;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // 原始纹理采样
                fixed4 original = tex2D(_MainTex, i.uv);
                
                // 毛玻璃模糊效果
                fixed4 blurred = GaussianBlur(_MainTex, i.uv, _MainTex_TexelSize.xy, _BlurSize);
                
                // 根据强度混合原始与模糊图像
                return lerp(original, blurred, _BlurStrength);
            }
            ENDCG
        }
    }
}
